#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>


int main(void)
{
    int ret = -1;

    printf("Hello tcp\r\n");

    int tcp_server = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in local_addr = {0};

    local_addr.sin_family = AF_INET;
    local_addr.sin_port = htons(6227);
    local_addr.sin_addr.s_addr = inet_addr("192.168.206.128");

    ret = bind(tcp_server, (const struct sockaddr *)&local_addr, sizeof(local_addr));

    if(ret < 0)
    {
        printf("bind errer");
    }
    else
    {
        printf("bing success\r\n");
    }

    ret = listen(tcp_server, 5);

    struct sockaddr_in client_addr;
    
    socklen_t addrlen = sizeof(client_addr);
    int client_fd = accept(tcp_server, (struct sockaddr *)&client_addr, &addrlen);

    char recvbuf[1024] = {0};
    ret = recv(client_fd, recvbuf, sizeof(recvbuf), 0);
    
    printf("recv from %s:%d\r\n", inet_ntoa(*(struct in_addr *)&client_addr.sin_addr.s_addr),  inet_ntoa(*(struct in_addr *)&client_addr.sin_port));
    printf("recv data: %s\r\n", recvbuf);

    char send_buf[1024] = "I have got the data:";
    strcat(send_buf, recvbuf);
    send(client_fd, send_buf, strlen(send_buf), 0);

    close(tcp_server);
    
    
}